package com.daowen.action;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.SQLQuery;
import org.hibernate.Query;
import com.daowen.dal.*;
import com.daowen.bll.*;
import com.daowen.entity.*;
import com.daowen.util.DaowenUIHelper;
import com.daowen.util.HibernateSessionFactory;
import com.daowen.util.ExtHelper;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.text.MessageFormat;
import com.daowen.util.PagerMetal;


public class NoticeAction extends PageActionBase {


    /********************************************************
     ****************** 信息注销监听支持*****************************
     *********************************************************/
    public void delete() {
        String id = request.getParameter("id");
        DALBase.delete("notice", " where id=" + id);
        binding();
    }

    /*************************************************************
     **************** 保存动作监听支持******************************
     **************************************************************/
    public void save() {
        String title = request.getParameter("title");
        String pubren = request.getParameter("pubren");
        String pubtime = request.getParameter("pubtime");

        String dcontent = request.getParameter("dcontent");
        SimpleDateFormat sdfnotice = new SimpleDateFormat("yyyy-MM-dd");
        Notice notice = new Notice();
        notice.setTitle(title == null ? "" : title);
        notice.setPubren(pubren == null ? "" : pubren);

        notice.setPubtime(new Date());

        notice.setClickcount(0);
        notice.setDcontent(dcontent == null ? "" : dcontent);
        DALBase.save(notice);

        try {
            response.sendRedirect(SystemParam.getSiteRoot()+"/admin/noticemanager.do?actiontype=get");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



    }

    /******************************************************
     *********************** 内部附件支持*********************
     *******************************************************/
    public void attachements(HttpServletRequest request,
                             HttpServletResponse response, String belongid) {
        DALBase.delete("attachement", MessageFormat
                .format(" where belongid=''{0}'' and belongtable=''notice'' ",
                        belongid));
        String[] photos = request.getParameterValues("fileuploaded");
        if (photos == null)
            return;
        for (int i = 0; i < photos.length; i++) {
            Attachement a = new Attachement();
            a.setType("images");
            a.setPubtime(new Date());
            a.setBelongfileldname("id");
            a.setFilename(photos[i]);
            a.setBelongid(belongid);
            a.setBelongtable("notice");
            a.setUrl(SystemParam.getSiteRoot() + "/upload/temp/"
                    + a.getFilename());
            a.setTitle(a.getFilename());
            DALBase.save(a);
        }
    }



    /******************************************************
     *********************** 更新内部支持*********************
     *******************************************************/
    public void update() {
        String id = request.getParameter("id");
        if (id == null)
            return;
        Notice notice = (Notice) DALBase.load(Notice.class, new Integer(id));
        if (notice == null)
            return;
        String title = request.getParameter("title");
        String pubren = request.getParameter("pubren");
        String pubtime = request.getParameter("pubtime");
        String dcontent = request.getParameter("dcontent");
        SimpleDateFormat sdfnotice = new SimpleDateFormat("yyyy-MM-dd");
        notice.setTitle(title);
        notice.setPubren(pubren);

        notice.setPubtime(new Date());

        notice.setDcontent(dcontent);
        DALBase.update(notice);

        try {
            response.sendRedirect(SystemParam.getSiteRoot()+"/admin/noticemanager.do?actiontype=get");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

    /******************************************************
     *********************** 加载内部支持*********************
     *******************************************************/
    public void load() {
        //
        String id = request.getParameter("id");
        String actiontype = "save";
        if (id != null) {
            Notice notice = (Notice) DALBase.load("notice", "where id=" + id);
            if (notice != null) {
                request.setAttribute("notice", notice);
            }
            actiontype = "update";
        }
        request.setAttribute("id", id);
        request.setAttribute("actiontype", actiontype);
        try {
            request.getRequestDispatcher("noticeadd.jsp").forward(request,
                    response);
        } catch (ServletException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /******************************************************
     *********************** 数据绑定内部支持*********************
     *******************************************************/
    public void binding() {
        String filter = "";
        //
        int pageindex = 1;
        int pagesize = 10;

        String title = request.getParameter("title");
        if (title != null)
            filter = "  where title like '%" + title + "%'  ";
        //
        // 获取当前分页
        String currentpageindex = request.getParameter("currentpageindex");
        // 当前页面尺寸
        String currentpagesize = request.getParameter("pagesize");
        // 设置当前页
        if (currentpageindex != null)
            pageindex = new Integer(currentpageindex);
        // 设置当前页尺寸
        if (currentpagesize != null)
            pagesize = new Integer(currentpagesize);
        List<Notice> listnotice = DALBase.getPageEnity("notice", filter,
                pageindex, pagesize);
        int recordscount = DALBase.getRecordCount("notice", filter == null ? ""
                : filter);
        request.setAttribute("listnotice", listnotice);
        PagerMetal pm = new PagerMetal(recordscount);
        // 设置尺寸
        pm.setPagesize(pagesize);
        // 设置当前显示页
        pm.setCurpageindex(pageindex);
        // 设置分页信息
        request.setAttribute("pagermetal", pm);
        // 分发请求参数
        dispatchParams(request, response);
        try {
            request.getRequestDispatcher("/admin/noticemanager.jsp").forward(
                    request, response);
        } catch (ServletException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
